"use strict";

function loadCart() {
  // 获取uid
  var uid = localStorage.getItem('uid');
  localStorage.getItem(uid); // 获取商品

  var url = 'http://jx.xuzhixiang.top/ap/api/cart-list.php';
  axios.get(url, {
    params: {
      id: uid
    }
  }).then(function (res) {
    var arr = res.data.data;
    var html = '';

    for (var i = 0; i < arr.length; i++) {
      html += "<div class=\"pronews\">\n            <input type=\"checkbox\" class=\"procheck\">\n            <img src=\"".concat(arr[i].pimg, "\" alt=\"\" class=\"\">\n            <a href=\"item.html?id=").concat(arr[i].pid, "\" class=\"pname\">").concat(arr[i].pname, "</a>\n            <span class=\"shop\">").concat(arr[i].pdesc, "</span>\n            <p class=\"unitprice\">").concat(arr[i].pprice, "</p>\n            <div class=\"cartamount\">\n                <input type=\"button\" value=\"-\" class=\"cartminus\">\n                <input type=\"text\" value=\"").concat(arr[i].pnum, "\" class=\"cartqua\">\n                <input type=\"button\" value=\"+\" class=\"cartplus\">\n            </div>\n            <p class=\"totalprice\">").concat(arr[i].pprice * arr[i].pnum, "</p>       \n            <input type=\"button\" value=\"\u5220\u9664\" data-id=\"").concat(arr[i].pid, "\" class=\"deltbtn\">    \n        </div>");
    }

    var odiv = document.querySelector(".box");
    odiv.innerHTML = html;
    var Minus = document.querySelectorAll(".cartminus");
    var qua = document.querySelectorAll(".cartqua");
    var Plus = document.querySelectorAll(".cartplus");
    var Totalprice = document.querySelectorAll(".totalprice");
    var Sigprice = document.querySelectorAll(".unitprice");

    function updataData(i) {
      Totalprice[i].innerText = qua[i].value * Sigprice[i].innerText;
      var obtn = document.querySelectorAll(".deltbtn");
      var pid = obtn[i].getAttribute("data-id");
      var url1 = 'http://jx.xuzhixiang.top/ap/api/cart-update-num.php';
      axios.get(url1, {
        params: {
          uid: uid,
          pid: pid,
          pnum: qua[i].value
        }
      }).then(function (res) {});
    } //减


    var _loop = function _loop(_i) {
      Minus[_i].onclick = function () {
        qua[_i].value--;

        if (qua[_i].value <= 0) {
          qua[_i].value = 1;
        }

        updataData(_i);
        gettotalprice();
      };
    };

    for (var _i = 0; _i < Minus.length; _i++) {
      _loop(_i);
    } // 加


    var _loop2 = function _loop2(_i2) {
      Plus[_i2].onclick = function () {
        qua[_i2].value++;
        updataData(_i2);
        gettotalprice();
      }; // 选中


      var obtn = document.querySelectorAll(".deltbtn");
      var acheck = document.querySelector(".overcheck");
      var sigcheck = document.querySelectorAll(".procheck");

      acheck.onclick = function () {
        for (var _i3 = 0; _i3 < sigcheck.length; _i3++) {
          sigcheck[_i3].checked = acheck.checked;
        }

        gettotalprice();
      }; // 全选


      for (var _i4 = 0; _i4 < sigcheck.length; _i4++) {
        sigcheck[_i4].onclick = function () {
          var count = 0;

          for (var j = 0; j < sigcheck.length; j++) {
            if (sigcheck[j].checked) {
              count++;
            }
          }

          if (count == sigcheck.length) {
            acheck.checked = true;
          } else {
            acheck.checked = false;
          }

          gettotalprice();
        };
      } // gettotalprice();
      // 删除商品


      var _loop3 = function _loop3(_i5) {
        obtn[_i5].onclick = function () {
          var pid = obtn[_i5].getAttribute("data-id");

          var obox = document.querySelectorAll(".pronews");
          var url = 'http://jx.xuzhixiang.top/ap/api/cart-delete.php';
          console.log(obtn[_i5].parentNode);
          axios.get(url, {
            params: {
              uid: uid,
              pid: pid
            }
          }).then(function (res) {
            odiv.removeChild(obox[_i5]);
            gettotalprice();
          });
        };
      };

      for (var _i5 = 0; _i5 < obtn.length; _i5++) {
        _loop3(_i5);
      }

      function gettotalprice() {
        var ototal = document.querySelector("#total");
        var o = 0;
        var sigcheck = document.querySelectorAll(".procheck");

        for (var _i6 = 0; _i6 < sigcheck.length; _i6++) {
          if (sigcheck[_i6].checked) {
            o += +Totalprice[_i6].innerText;
          }
        }

        ototal.innerText = o;
      }
    };

    for (var _i2 = 0; _i2 < Plus.length; _i2++) {
      _loop2(_i2);
    }
  })["catch"](function (err) {});
}

loadCart();